package com.knowledge.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.knowledge.common.constant.GlobalConstant;
import com.knowledge.common.enums.NewsTypeEnum;
import com.knowledge.dao.entity.NewsEntity;
import com.knowledge.dao.mapper.NewsMapper;
import com.knowledge.service.NewsService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.knowledge.utils.TemplateParserUtil;
import com.knowledge.vo.Result;
import com.knowledge.vo.params.NewsParam;
import com.knowledge.vo.params.PageParams;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;

/**
 * <p>
 * 消息表 服务实现类
 * </p>
 *
 * @author zzz
 * @since 2022-03-03
 */
@Service
public class NewsServiceImpl extends ServiceImpl<NewsMapper, NewsEntity> implements NewsService {

    @Override
    public Result listNewsWithPage(NewsParam newsParam, PageParams pageParams) {
        Page<NewsEntity> page = new Page<>(pageParams.getPage(), pageParams.getPageSize());
        QueryWrapper<NewsEntity> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("user_id", newsParam.getUserId());
        queryWrapper.orderByAsc("is_read");
        queryWrapper.orderByDesc("create_time");
        page = page(page, queryWrapper);
        return Result.success(page.getRecords());
    }

    @Override
    public void addNews(NewsEntity newsEntity) {
        newsEntity.setCreateTime(new Date());
        newsEntity.setUpdateTime(new Date());
        newsEntity.setIsRead(GlobalConstant.NO);
        this.save(newsEntity);
    }

    @Override
    public Boolean updateToRead(Integer id) {
        UpdateWrapper<NewsEntity> updateWrapper = new UpdateWrapper<>();
        updateWrapper.lambda().set(NewsEntity::getIsRead, GlobalConstant.YES).eq(NewsEntity::getId, id);
        return update(updateWrapper);
    }
}
